package com.haowu.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haowu.common.core.utils.StringUtils;
import com.haowu.common.mybatis.core.page.PageQuery;
import com.haowu.common.mybatis.core.page.TableDataInfo;
import com.haowu.customer.domain.bo.HwAppSocialAuthBo;
import com.haowu.customer.domain.vo.HwAppSocialAuthVo;
import com.haowu.customer.mapper.HwAppSocialAuthMapper;
import com.haowu.customer.api.domain.HwAppSocialAuth;
import com.haowu.customer.service.IHwAppSocialAuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 第三方授权登录Service业务层处理
 *
 * @author lky
 * @date 2023-02-13
 */
@RequiredArgsConstructor
@Service
public class HwAppSocialAuthServiceImpl implements IHwAppSocialAuthService {

    private final HwAppSocialAuthMapper baseMapper;

    /**
     * 查询第三方授权登录
     */
    @Override
    public HwAppSocialAuthVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询第三方授权登录列表
     */
    @Override
    public TableDataInfo<HwAppSocialAuthVo> queryPageList(HwAppSocialAuthBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HwAppSocialAuth> lqw = buildQueryWrapper(bo);
        Page<HwAppSocialAuthVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询第三方授权登录列表
     */
    @Override
    public List<HwAppSocialAuthVo> queryList(HwAppSocialAuthBo bo) {
        LambdaQueryWrapper<HwAppSocialAuth> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HwAppSocialAuth> buildQueryWrapper(HwAppSocialAuthBo bo) {
        LambdaQueryWrapper<HwAppSocialAuth> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, HwAppSocialAuth::getUid, bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthType()), HwAppSocialAuth::getAuthType, bo.getAuthType());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthIdentification()), HwAppSocialAuth::getAuthIdentification, bo.getAuthIdentification());
        lqw.eq(StringUtils.isNotBlank(bo.getUnionId()), HwAppSocialAuth::getUnionId, bo.getUnionId());
        lqw.eq(bo.getIsDelete() != null, HwAppSocialAuth::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增第三方授权登录
     */
    @Override
    public Boolean insertByBo(HwAppSocialAuthBo bo) {
        HwAppSocialAuth add = BeanUtil.toBean(bo, HwAppSocialAuth.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改第三方授权登录
     */
    @Override
    public Boolean updateByBo(HwAppSocialAuthBo bo) {
        HwAppSocialAuth update = BeanUtil.toBean(bo, HwAppSocialAuth.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HwAppSocialAuth entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除第三方授权登录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
